#include "stm32F1xx_hal.h" 
#include "stm32F1xx_hal_conf.h" 
#include "DS18b20.h"
/*********************************** 
//????: void delay(unsigned int uSeconds) 
9
//????: 
//? ? ?: ? 
//????: ?? 
//????: 100=10us 
***********************************/ 
void delay(unsigned int k) //
{ 
 unsigned int i,j; 
 for(i=0;i<k*8;i++);
 for(j=0;j<10;j++);
 } 
 
/******************************************************************************
* 
* ? ? ? : SDA_OUT 
* ???? : SDA ????? 
* ? ? : ? 
* ? ? : ? 
*******************************************************************************
*/ 
void DQ_OUT(void) 
{ 
 GPIO_InitTypeDef GPIO_InitStruct; 
 GPIO_InitStruct.Pin = GPIO_PIN_14; 
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 
 GPIO_InitStruct.Speed=GPIO_SPEED_FREQ_HIGH; 
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 
} 
/******************************************************************************
* 
* ? ? ? : SDA_IN 
* ???? : SDA ????? 
* ? ? : ? 
* ? ? : ? 
*******************************************************************************
*/ 
void DQ_IN(void) 
{ 
 GPIO_InitTypeDef GPIO_InitStruct; 
 GPIO_InitStruct.Pin = GPIO_PIN_14; 
 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 
 GPIO_InitStruct.Speed=GPIO_SPEED_FREQ_HIGH; 
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 
} 
/*********************************** 
//????: unsigned char ow_reset(void) 
//????: 
//? ? ?: ? 
//????: ?? 
//????: 
***********************************/ 
unsigned char ow_reset(void) // 0=presence, 1 = no part 
{ 
unsigned char presence; 
 DQ_L; // pull DQ line low to start write timeslot 
 delay(480); //640 // leave it low for at least 480us,here 480us 
 DQ_H; // allow line to return high 
 delay(72);//94 // wait for presence 72us 
 DQ_IN(); 
 if(if_DQ_H){presence = 1;} 
 else presence = 0;// get presence signal 
 delay(480); //640 
 DQ_OUT(); // wait for end of timeslot 
 return(presence); // presence signal returned 
} 
unsigned char read_byte(void) 
{ 
unsigned char i; 
unsigned char value = 0; 
 for (i=8;i>0;i--) 
 { 
 value>>=1; 
 DQ_L;// pull DQ low to start read timeslot 
 DQ_H;// then rlease DQ 
 DQ_IN(); 
 delay(6);//5 // read DQ data at 1 to 15us,here delay 6us; 
 if(if_DQ_H) 
 value|=0x80; 
 delay(72);//72 
 DQ_OUT();// wait for rest of timeslot,72us 
 } 
 return(value); 
} 
/*********************************** 

***********************************/ 
void write_byte(char val) 
{ 
unsigned char i; 
 DQ_OUT(); 
 for (i=8; i>0; i--) // writes byte, one bit at a time 
 { 
 DQ_L; // pull DQ low to start timeslot 
 if(( val&0x01)==0x01){DQ_H;} 
 else DQ_L; 
 delay(72);//72 // hold value for remainder of timeslot,here 72us 
 DQ_H;// 
 val=val/2; 
 } 
 delay(20); 
} 
/*********************************** 

***********************************/ 
float Read_Temperature(void) 
{ 
unsigned char Hdata,Ldata,b; 
int a; 
unsigned char flag; 
float x,y,z; 
 ow_reset(); 
 write_byte(0xCC); // Skip ROM 
 write_byte(0xBE); // Read Scratch Pad 
 delay(100); //?????????????? 
 Ldata=read_byte(); // Low byte first 
 Hdata=read_byte(); // High byte after 
 ow_reset(); 
 write_byte(0xCC); //Skip ROM 
 write_byte(0x44); // Start Conversion 
 a=Hdata*256+Ldata; 
 x=(float)(Ldata&0x0f); 
 x=x/16; 
 if(a<0)flag=1; 
 else flag=0; 
 b=a>>4; 
 z=(float)(b); 
// a=b&0x007f; 
 if(flag==1) 
 { 
 b=~b+1; 
 z=(float)(b); 
 z=0-z; 
 } 
 y=z+x; 
 return y; 
}

